var BaseLoginMain = app.controller('BaseLoginMain', function ($rootScope, $scope, $http, $state, LoginMainDefault,BaseLoginMainData) {
	try {
		try{
			Wechat.isInstalled({scope: 'snsapi_userinfo'}, 
					function onSuccess(suc) {
				$rootScope.inswx=true;
			},
			function onError(err) {
				$rootScope.inswx=false;
			}
			);

			YCQQ.checkClientInstalled(
					function onSuccess(suc) {
						$rootScope.insqq=true;
					},
					function onError(err) {
						$rootScope.insqq=false;
					}
			);
		}catch (e) {}
		
		$rootScope.showf = false;
		$rootScope.title = '用户登录';
		$scope.slides = [];
		$scope.slides = LoginMainDefault;

		$(".main_image").height($(window).height()-120);
		
		setTimeout(function () {
			$dragBln = false;
			$(".main_image").touchSlider({
				flexible : true,
				speed : 200,
				paging : $(".flicking_con a"),
				counter : function (e){
					$(".flicking_con a").removeClass("on").eq(e.current-1).addClass("on");
				}
			});
			$(".main_image").bind("mousedown", function() {
				$dragBln = false;
			});
			$(".main_image").bind("dragstart", function() {
				$dragBln = true;
			});
			$(".main_image a").click(function(){
				if($dragBln) {
					return false;
				}
			});
			timer = setInterval(function(){
				$("#btn_next").click();
			}, 5000);
			$(".main_visual").hover(function(){
				clearInterval(timer);
			},function(){
				timer = setInterval(function(){
					$("#btn_next").click();
				},5000);
			});
			$(".main_image").bind("touchstart",function(){
				clearInterval(timer);
			}).bind("touchend", function(){
				timer = setInterval(function(){
					$("#btn_next").click();
				}, 5000);
			});
			
			$(".loginmain").height($(window).height());
			$(".main_image").height($(window).height()-120);
			//$(".flicking_con").height($(window).height()-140);
			$(".main_image img").height($(window).height()-120);
			$(".loginmain").css("overflow","hidden");
		}, 500);

		//微信用户登录
		$scope.wxlogintimer = null;
		$scope.wxlogin = function () {
			if(!$rootScope.inswx){
				showcatch("您还没有安装微信");
			}
			
			ProgressIndicator.showSimpleWithLabel(false, '正在启动微信...');
			$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 10000);
			Wechat.auth({scope: 'snsapi_userinfo'},
					function onSuccess(suc) {
				if ($scope.wxlogintimer != null)
					window.clearTimeout($scope.wxlogintimer);
				ProgressIndicator.hide();
				ProgressIndicator.showSimpleWithLabel(false, '正在获取微信登录信息...');

				/*
				 * wxlogin suc
                     Object
                     code: "0113e012ffd82c0f6d194900b7d1ae4s"
                     country: "CN"
                     lang: "zh_CN"
                     state: "wechat_auth"
                     __proto__: —
				 * */
				var url = SITE_URL + "index.php?app=userapp&act=wxregister&callback=JSON_CALLBACK&code=" + suc.code;
				$http.jsonp(url).success(function (data) {
					if (data.done) {
						
						//统计绑定注册用户
						razor('user','BaseLoginMain',{'uid':data.retval.user_id});
						
						var requestData = {};
						requestData.username = data.retval.user_id;
						requestData.password = '123456';
						requestData.nickname = data.retval.real_name;

						$.ajax({
							url: 'https://a1.easemob.com/darven02/dscj/users',
							type: 'POST',
							data: JSON.stringify(requestData),
							headers: {
								'Content-Type': 'application/json'
							},
							success: function (respData) {
								console.log(respData);

							},
							error: function (respData) {
								console.log(respData);
							},
						});

						window.localStorage.u = data.retval.user_name;
						window.localStorage.p = data.retval.password;


						try {
							easemobchat.login(
									{username: data.retval.user_id, password: '123456'},
									function (s) {
									},
									function (e) {
										console.log(e);
									}
							);
						} catch (e) {
							showcatch(e);
						}

						url = SITE_URL + "index.php?app=userapp&act=checkuser&callback=JSON_CALLBACK&u=" + window.localStorage.u + "&p=" + window.localStorage.p;
						$http.jsonp(url).success(function (data) {
							if (data.done) {
								ProgressIndicator.hide();
								var d = data.retval;
								window.localStorage.user = JSON.stringify(d);
								$rootScope.go("index");

								$cordovaChat.init(d.BOSH_SERVICE, d.CHATURI, d.CHATUSER, d.CHATUSERP);
								$cordovaChat.ChatConnect();
							}
							else {
								ProgressIndicator.hide();
								console.log("微信登录失败，请重试1");
							}
						}).error(function () {
							ProgressIndicator.hide();
							console.log("微信登录失败，请重试2");
						});
					}
					else {
						ProgressIndicator.hide();
						console.log("微信登录失败，请重试4");
					}
				}).error(function () {
					ProgressIndicator.hide();
					console.log("微信登录失败，请重试5");
				});
				console.log("wxlogin suc", suc);
			},
			function onError(err) {
				if ($scope.wxlogintimer != null)
					window.clearTimeout($scope.wxlogintimer);
				ProgressIndicator.hide();
				/*
				 * wxlogin err ERR_USER_CANCEL
				 * */
				console.log("登录失败" + err);
			});
		};
	}
	catch (e) {
		showcatch(e);
	}
});

//默认值设置
BaseLoginMain.LoginMainDefault = function ($q,$http) {
	var def = $q.defer();
	
	var url = SITE_URL + "index.php?app=userapp&act=ad1&callback=JSON_CALLBACK";
	$http.jsonp(url).success(function (data) {
		def.resolve(data.retval);
	});

	return def.promise;
}


BaseLoginMain.BaseLoginMainData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u!=undefined&&window.localStorage.u!=null&&window.localStorage.u!=""&&window.localStorage.p!=undefined&&window.localStorage.p!=null&&window.localStorage.p!=""){			
			$rootScope.go("index");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}